<?php
namespace app\common\model;




use app\common\service\App;
use app\common\service\AppService;
use think\Model;

class UserFollow extends Model
{

    public function getRows($map = [])
    {

        $rows =  $this->alias('a')
            ->join('user_extend e','e.user_id = a.user_id','LEFT')
            ->join('users u','u.user_id = a.user_id','LEFT')
            ->join('room r','r.room_id = e.room_id','LEFT')
            ->where($map)
            ->order('a.update_time desc')
            ->field('u.user_id,u.nick_name,u.header_img,u.tag,e.room_id,e.online_status,e.online_time,e.noble_id,e.noble_time,e.level,r.img,r.is_lock,r.password,a.update_time')
            ->paginate()->each(function ($item){
                $item['noble_id'] = \app\api\controller\Base::checkNoble($item);
                if ($item['room_id'] == 0){
                    $item['img']      = '';
                    $item['password'] = '';
                    $item['is_lock']  = 0;
                }
//                if ($item['online_status']){
//                    $item['status'] = '当前在线';
//                }else{
                    $item['status'] = formatTime($item['update_time'],false);
//                }
                $item['user_id'] = hashid($item['user_id']);
            });

        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];

    }

    public function getRows_v1($map = [])
    {
        $log = AppService::getUserInfo('log',$map['a.follow_user']);
        $lat = AppService::getUserInfo('lat',$map['a.follow_user']);
        $rows =  $this->alias('a')
            ->join('user_extend e','e.user_id = a.user_id','LEFT')
            ->join('users u','u.user_id = a.user_id','LEFT')
            ->join('room r','r.room_id = e.room_id','LEFT')
            ->where($map)
            ->order('a.update_time desc')
            ->field("u.sex,u.birthday,(st_distance (point (e.log,e.lat),point($log,$lat) ) / 0.0111) AS distance,u.user_id,u.nick_name,u.header_img,u.tag,e.room_id,e.online_status,e.online_time,e.noble_id,e.noble_time,e.level,r.img,r.is_lock,r.password,a.update_time")
            ->paginate()->each(function ($item){
                $item['noble_id'] = \app\api\controller\Base::checkNoble($item);
                if ($item['room_id'] == 0){
                    $item['img']      = '';
                    $item['password'] = '';
                    $item['is_lock']  = 0;
                }
//                if ($item['online_status']){
//                    $item['status'] = '当前在线';
//                }else{
                $item['status'] = formatTime($item['update_time'],false);
//                }
                $item['user_id'] = hashid($item['user_id']);
            });

        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];

    }

    public function getOnline($map = [])
    {

        return $this->alias('a')
            ->join('user_extend e','e.user_id = a.user_id','LEFT')
            ->where($map)
            ->field('a.user_id')
            ->limit(1000)
            ->select();
    }



    public function getFollow($map = [])
    {

        $rows =  $this->alias('a')
            ->join('user_extend e','e.user_id = a.follow_user','LEFT')
            ->join('users u','u.user_id = a.follow_user','LEFT')
            ->join('room r','r.room_id = e.room_id','LEFT')
            ->where($map)
            ->order('a.update_time desc')
            ->field('u.user_id,u.nick_name,u.header_img,u.tag,e.room_id,e.online_status,e.online_time,e.noble_id,e.noble_time,e.level,r.img,r.is_lock,r.password,a.update_time')
            ->paginate()->each(function ($item){
               
                $item['noble_id'] = \app\api\controller\Base::checkNoble($item);
                if ($item['room_id'] == 0){
                    $item['img']      = '';
                    $item['password'] = '';
                    $item['is_lock']  = 0;
                }
//                if ($item['online_status']){
//                    $item['status'] = '当前在线';
//                }else{
                    $item['status'] = formatTime($item['update_time'],false);
//                }



                $item['user_id'] = hashid($item['user_id']);
            });

        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];

    }

    public function getFollow_v1($map = [])
    {
        $log = AppService::getUserInfo('log',$map['a.user_id']);
        $lat = AppService::getUserInfo('lat',$map['a.user_id']);
        $rows =  $this->alias('a')
            ->join('user_extend e','e.user_id = a.follow_user','LEFT')
            ->join('users u','u.user_id = a.follow_user','LEFT')
            ->join('room r','r.room_id = e.room_id','LEFT')
            ->where($map)
            ->order('a.update_time desc')
            ->field("u.sex,u.birthday,(st_distance (point (e.log,e.lat),point($log,$lat) ) / 0.0111) AS distance,u.user_id,u.nick_name,u.header_img,u.tag,e.room_id,e.online_status,e.online_time,e.noble_id,e.noble_time,e.level,r.img,r.is_lock,r.password,a.update_time,r.room_name")
            ->paginate()->each(function ($item){
                $item['age'] = getAge($item['birthday']);
                $item['noble_id'] = \app\api\controller\Base::checkNoble($item);
                if ($item['room_id'] == 0){
                    $item['img']      = '';
                    $item['password'] = '';
                    $item['is_lock']  = 0;
                }
//                if ($item['online_status']){
//                    $item['status'] = '当前在线';
//                }else{
                $item['status'] = formatTime($item['update_time'],false);
//                }



                $item['user_id'] = hashid($item['user_id']);
            });

        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];

    }


    public function getGroup($map = [])
    {

        $rows =  $this->alias('a')
//            ->join('user_extend e','e.user_id = a.user_id','LEFT')
            ->join('users u','u.user_id = a.user_id','LEFT')
            ->where($map)
            ->field('u.user_id,u.nick_name,u.header_img,u.uuid')
            ->cache(5)
            ->select();

        foreach ($rows as $k => $v){
//            if ($v['online_status']){
//                $rows[$k]['status'] = '当前在线';
//            }else{
//                $rows[$k]['status'] = formatTime($v['online_time']);
//            }
            $rows[$k]['user_id'] = hashid($v['user_id']);
        }
        return groupByInitials( collection($rows)->toArray(),'nick_name');

    }

}